﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Sita.Data.Context;
using Sita.Entities;
using Sita.Data.Core;

namespace Sita.Data.Repositories
{
    public class DeliveryNoteRepository : GenericRepository<DeliveryNote>
    {
        public DeliveryNoteRepository() :
            base(new SitaContext())
        { }

        public IList<DeliveryNote> GetPaginatedByBusinessUnitID(int businessUnitID, int pageIndex, int pageSize,
                                                                string sortColumn, bool ascending, out int totalRows)
        {
            RepositoryHelper<DeliveryNote> Helper = new RepositoryHelper<DeliveryNote>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<DeliveryNote> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).DeliveryNotes
                .Where(dn => dn.BusinessUnitID == businessUnitID)
                .Select(dn => dn);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<DeliveryNote>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList()
                                :
                                    ((IOrderedQueryable<DeliveryNote>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList();

        }

        public DeliveryNote GetByIDWithItemsAndCustomer(int DeliveryNoteID)
        {
            DeliveryNote ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).DeliveryNotes
                .Where(dn => dn.DeliveryNoteID == DeliveryNoteID)
                .Include("DeliveryNoteReplacements")
                .Include("DeliveryNoteReplacements.Replacement")
                .Include("Customer")
                .Include("BusinessUnit")
                .SingleOrDefault();

            return ReturnSet;
        }

        public DeliveryNote GetByIDWithItems(int DeliveryNoteID)
        {
            DeliveryNote ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).DeliveryNotes
                .Where(dn => dn.DeliveryNoteID == DeliveryNoteID)
                .Include("DeliveryNoteReplacements")
                .SingleOrDefault();

            return ReturnSet;
        }

        public IList<DeliveryNote> GetAvailableByCustomerID(int customerID)
        {
            IList<DeliveryNote> ReturnSet;
            ReturnSet = (base.StoreContext as ISitaContext).DeliveryNotes
                .Where(dn => dn.CustomerID == customerID && dn.InvoiceDeliveryNotes.Count() == 0)
                .ToList();

            return ReturnSet;
        }

        public IList<DeliveryNote> GetPaginatedByFilter(int businessUnitID, int pageIndex, int pageSize,
                                                                string sortColumn, bool ascending, int customerID, int deliveryNoteID, out int totalRows)
        {
            RepositoryHelper<DeliveryNote> Helper = new RepositoryHelper<DeliveryNote>();
            var OrderByExpr = Helper.GetOrderExpression(sortColumn);

            IQueryable<DeliveryNote> ReturnSet;

            ReturnSet = (base.StoreContext as ISitaContext).DeliveryNotes
                .Where(dn => dn.BusinessUnitID == businessUnitID)
                .Where(dn => customerID == 0 || dn.CustomerID == customerID)
                .Where(dn => deliveryNoteID == 0 || dn.DeliveryNoteID == deliveryNoteID)
                .Select(dn => dn);

            totalRows = ReturnSet.Count();

            return (ascending)
                                ?
                                    ((IOrderedQueryable<DeliveryNote>)System.Linq.Queryable.OrderBy(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList()
                                :
                                    ((IOrderedQueryable<DeliveryNote>)System.Linq.Queryable.OrderByDescending(ReturnSet, OrderByExpr))
                                        .Skip(pageIndex * pageSize)
                                        .Include("Customer")
                                        .Take(pageSize)
                                        .ToList();

        }
    }
}